package com.zhx.dao;

import com.zhx.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/**
 * @program: userdao
 * @author: zhx
 * @create: 2021-03-22 20:13
 **/
@Repository //通过Spring注解定义一个DAO
public class UserDao {
    private JdbcTemplate jdbcTemplate;
    private final static String MATCH_COUNT_SQL="select count(*) from t_user where user_name=? and password=?";
    private final static String UPDATE_LOGIN_INFO_SQL = "update t_user set last_visit=?,last_ip=?,credits=? where user_id=?";
    private final static String QUERY_USER_SQL = "select * from t_user where user_name=?";
    private final static String INSERT_USER_SQL="insert into t_user(user_name,password,credits,last_ip,last_visit,register_time) values(?,?,?,?,?,?)";
    @Autowired //自动注入jdbctemplate的bean
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate=jdbcTemplate;
    }

    /**
     * 根据用户名和密码来获取查到的个数
     * @param userName
     * @param passWord
     * @return
     */
    public int getMatchCount(final String userName,final String passWord){
        int count = jdbcTemplate.queryForObject(MATCH_COUNT_SQL, new Object[]{userName, passWord}, Integer.class);
        return count;
    }

    /**
     * 根据用户名获取用户信息
     * @param userName
     * @return
     */
    public User findUserByUserName(final String userName){
        final User user = new User();
        jdbcTemplate.query(QUERY_USER_SQL, new Object[]{userName}, new RowCallbackHandler() {
            @Override
            public void processRow(ResultSet resultSet) throws SQLException {
                user.setUserId(resultSet.getInt("user_id"));
                user.setUserName(userName);
                user.setCredits(resultSet.getInt("credits"));
            }
        });
        return user;
    }

    /**
     * 更新登陆表
     * @param user
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public User updateLoginInfo(User user){
        try {
            jdbcTemplate.update(UPDATE_LOGIN_INFO_SQL, new Object[]{user.getLastvisit(), user.getLastIp(), user.getCredits(), user.getUserId()});
            return user;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 进行插入数据，向user表
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED)
    public User insertUser(User user) {
        int update = 0;
        try {
            update = jdbcTemplate.update(INSERT_USER_SQL, new Object[]{user.getUserName(), user.getPassWord(), user.getCredits(),user.getLastIp(),user.getLastvisit(),user.getRegisterTime()});
            if (update!=0){
                User user1 = findUserByUserName(user.getUserName());
                user.setUserId(user1.getUserId());
                return user;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
